package com.yanze.schoolinformation.model.http;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.http.service.DataService;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 构造Retrofit请求
 * Created by MrZheng on 2018/4/4.
 */

public class RetrofitClient {

    //超时时间
    public static long DEFALUT_TIMEOUT = 8;

    public static RetrofitClient mDataClient;
    public static Retrofit mDouban;
    public static Retrofit mGanhuo;
    public static Retrofit mWanAndroid;
    public static Retrofit mQsbk;
    public static Retrofit mSchool;

    private Retrofit.Builder mBuilder;

    //构造方法
    private RetrofitClient(String baseUrl) {
        createBuilder();
        if (baseUrl.equals(C.url.API_DOUBAN)) {
            if (mDouban == null) {
                synchronized (RetrofitClient.class) {
                    if (mDouban == null) {
                        mDouban = mBuilder
                                .baseUrl(baseUrl)
                                .build();
                    }
                }
            }
        } else if (baseUrl.equals(C.url.API_GANKIO)) {
            if (mGanhuo == null) {
                synchronized (RetrofitClient.class) {
                    if (mGanhuo == null) {
                        mGanhuo = mBuilder
                                .baseUrl(baseUrl)
                                .build();
                    }
                }
            }
        } else if (baseUrl.equals(C.url.API_WAN_ANDROID)) {
            if (mWanAndroid == null) {
                synchronized (RetrofitClient.class) {
                    if (mWanAndroid == null) {
                        mWanAndroid = mBuilder
                                .baseUrl(baseUrl)
                                .build();
                    }
                }
            }
        } else if (baseUrl.equals(C.url.API_QSBK)) {
            if (mQsbk == null) {
                synchronized (RetrofitClient.class) {
                    if (mQsbk == null) {
                        mQsbk = mBuilder
                                .baseUrl(baseUrl)
                                .build();
                    }
                }
            }
        } else {
            mSchool = mBuilder
                    .baseUrl(baseUrl)
                    .build();
        }
    }

    public void createBuilder() {
        synchronized (RetrofitClient.class) {
            if (mBuilder == null) {
                OkHttpClient client = new OkHttpClient.Builder()
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request = chain.request();
                                Response proceed = chain.proceed(request);
                                return proceed;
                            }
                        }).connectTimeout(DEFALUT_TIMEOUT, TimeUnit.SECONDS)
                        .writeTimeout(DEFALUT_TIMEOUT, TimeUnit.SECONDS)
                        .readTimeout(DEFALUT_TIMEOUT, TimeUnit.SECONDS)
                        .build();
                mBuilder = new Retrofit.Builder()
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .client(client);
            }
        }
    }

    //创建单例对象
    public static RetrofitClient getDataClient(String baseUrl) {
        mDataClient = new RetrofitClient(baseUrl);
        return mDataClient;
    }

    //////创建代理对象////
    private static <T> T createData(String baseUrl, Class<T> apiService) {
        if (baseUrl.equals(C.url.API_DOUBAN)) {
            return RetrofitClient.getDataClient(baseUrl).mDouban.create(apiService);
        } else if (baseUrl.equals(C.url.API_GANKIO)) {
            return RetrofitClient.getDataClient(baseUrl).mGanhuo.create(apiService);
        } else if (baseUrl.equals(C.url.API_WAN_ANDROID)) {
            return RetrofitClient.getDataClient(baseUrl).mWanAndroid.create(apiService);
        } else if (baseUrl.equals(C.url.API_QSBK)) {
            return RetrofitClient.getDataClient(baseUrl).mQsbk.create(apiService);
        } else {
            return RetrofitClient.getDataClient(baseUrl).mSchool.create(apiService);
        }
    }

    /////链式调用，返回代理对象
    public static DataService DATA(String baseUrl) {
        return createData(baseUrl, DataService.class);
    }
}
